/** 战斗回路,按键设定为:单发a,单发b

  By setycyas @2025-08-01
*/

/* 全局设定
*/ 
;导入自定义函数库
#Include "..\Class\OrderMap.2.ahk"
#Include "..\Class\MyFuns.2.ahk"
;导入宏相关函数和UI
#Include "..\Class\FaceDetector.2.ahk"
#Include "..\Class\CommonMacro.2.ahk"
#Include "..\Class\CommonUI.2.ahk"
#Include "..\Class\MyEmu.2.ahk"
#Include "..\Class\MySecondCounter.2.ahk"

#Warn
#MaxThreadsPerHotkey 1
SetWorkingDir(A_ScriptDir)

SendMode("Input")

/* 制作战斗回路的宏类
*/ class BatcirMacro extends CommonMacro {

  ;计时器类
  _secCounter := MySecondCounter(ObjBindMethod(this, "_count"))
  ;_secCounter := MySecondCounter()

  /* 计时函数 */
  _count(){
  	this.UI._statusBar.SetText(this._secCounter.getCount())
  }

  /* 新增的宏函数
  */
  ;植物左右连打技能
  _植物左右连打(key){
    static directions := ["left", "right"]
    local isPressingRight, isPressingLeft, directionIndex
    isPressingRight := MyFuns.getKeyState("right")
    isPressingLeft := MyFuns.getKeyState("left")
    directionIndex := isPressingRight ? 1 : 2
    ;如果左右键都没有按,默认加按一下右键,否则只按拳
    if (!isPressingRight and !isPressingLeft)
      MyFuns.pressKeys(["right", this._keys[1]], [50, 150])
    else
      MyFuns.press(this._keys[1], 150)
    loop 10 {
      MyFuns.press(directions[directionIndex], 150)
      if (!MyFuns.getKeyState(key))
        break
      directionIndex := (directionIndex == 1) ? 2 : 1
    }
  }
  ;植物吞人技能,成功率一般般
  _植物吞人(key){
    local face, back
    face := this._face.face()
    back := this._face.back()
    if (face == "right"){
      MyFuns.keyDown("down")
      sleep(30)
      MyFuns.keyDown(face)
      sleep(50)
      MyFuns.keyUp("down")
      sleep(80)
      MyFuns.keyDown("up")
      sleep(80)
      MyFuns.keyUp(face)
      sleep(50)
      MyFuns.keyDown(back)
      sleep(20)
      MyFuns.keyUp("up")
      sleep(80)
      this._down(1)
      sleep(30)
      MyFuns.keyUp(back)
      sleep(30)
      this._up(1)
    } else {
      MyFuns.keyDown("down")
      sleep(110)
      MyFuns.keyDown(face)
      sleep(30)
      MyFuns.keyUp("down")
      sleep(80)
      MyFuns.keyDown("up")
      sleep(50)
      MyFuns.keyUp(face)
      sleep(30)
      MyFuns.keyDown(back)
      sleep(60)
      MyFuns.keyUp("up")
      sleep(50)
      this._down(1)
      sleep(30)
      MyFuns.keyUp(back)
      sleep(80)
      this._up(1)
    }
  }
  _下上A改(key){
    send("{down down}")
    sleep(30)
    send("{down up}")
    sleep(30)
    send("{up down}")
    sleep(30)
    this._press(1)
    sleep(30)
    send("{up up}")
  }
  ;下上A之后连打
  _下上A连打(key){
    this._下上A改(key)
    sleep(50)
    if (MyFuns.getKeyState(key)) {
      this._连发A(key)
    }
  }
  ;前A之后连打
  _前A连打(key){
    this._前A(key)
    sleep(50)
    if (MyFuns.getKeyState(key)) {
      this._连发A(key)
    }
  }
  _连发A改(key){
    this._press(1)
    sleep(100)
    if (MyFuns.getKeyState(key)) {
      this._连发A(key)
    }
  }
  _前A改(key){
    local face
    face := this._face.face()
    send("{" . face . " down}")
	  sleep(50)
    this._down(1)
    sleep(190)
    this._up(1)
    send("{" . face . " up}")
    sleep(300)
  }
  _蓄力中AB(key){
    this._up(1)
    sleep(30)
    this._down(1)
    this._down(2)
    sleep(50)
    this._up(1)
    this._up(2)
  }
  _鸵鸟低浮空(key){
    this._press(2)
    sleep(50)
    send("{up down}")
    sleep(20)
    this._press(2)
    sleep(20)
    send("{up up}")
  }
  _鸵鸟高浮空(key){
    this._press(2)
    sleep(100)
    send("{up down}")
    sleep(20)
    this._press(2)
    sleep(20)
    send("{up up}")
  }
  _单发拳(key){
    this._press(1)
  }
  /*
  _游聚专用ab连发(key){
    loop 3 {
      send("{p down}")
      sleep(30)
      send("{p up}")
      sleep(30)
    }
  }
  */
  _游聚专用ab连发(key){
    local vk := GetKeyVK(MyFuns.getHotkey(key))
    loop {
      MyFuns.press("p")
      sleep(30)
    } until(not (MyFuns.getVkState(vk)))
    ;MyFuns.press("p")
    ;vk := GetKeyVK(MyFuns.getHotkey(key))
    ;while (MyFuns.getVkState(vk)) {
    ;  MyFuns.press("p")
    ;  sleep(30)
    ;}
  }
  ;旋转摇,摆脱被抓用
  _旋转摇(key){
    static directions := ["up", "left", "down", "right"]
    local vk := GetKeyVK(MyFuns.getHotkey(key))
    local i := 1
    loop {
      MyFuns.keyDown(directions[i])
      this._down(1)
      sleep(20)
      MyFuns.keyUp(directions[i])
      this._up(1)
      sleep(20)
      i := i+1
      if (i > 4) {
        i := 1
      }
    } until(not (MyFuns.getVkState(vk)))
  }
  ;兼容蓄力情况的ab,如果在蓄力中使用会变成跳起再放开蓄力再ab
  _ab(key){
    this._down(1)
    this._down(2)
    sleep(30)
    this._up(2)
    sleep(30)
    this._up(1)
    sleep(30)
    this._down(1)
    this._down(2)
    sleep(30)
    this._up(1)
    this._up(2)
  }
  ;新增计时函数
  _计时(key){
  	this._secCounter.toggle()
  }

  /* 重载宏接口
  */
  _setMacro(){
    local obm := ObjBindMethod ;函数名简写,制作对象绑定函数
    ;制作宏相关Map
    this.macroMap := OrderMap()
    this.macroMap["连发A"] := obm(this, "_连发A改")
    ;this.macroMap["连发A高效"] := obm(this, "_连发A高效")
    this.macroMap["下上A连打"] := obm(this, "_下上A连打")
    this.macroMap["下上A改"] := obm(this, "_下上A改")
    this.macroMap["前A连打"] := obm(this, "_前A连打")
    this.macroMap["前A改"] := obm(this, "_前A改")
    this.macroMap["下前A"] := obm(this, "_下前A")
    this.macroMap["蓄力A"] := obm(this, "_蓄力A")
    this.macroMap["植物左右连打"] := obm(this, "_植物左右连打")
    this.macroMap["植物吞人"] := obm(this, "_植物吞人")
    this.macroMap["鸵鸟低浮空"] := obm(this, "_鸵鸟低浮空")
    this.macroMap["鸵鸟高浮空"] := obm(this, "_鸵鸟高浮空")
    this.macroMap["单发拳"] := obm(this, "_单发拳")
    this.macroMap["后跳"] := obm(this, "_后跳")
    this.macroMap["蓄力中AB"] := obm(this, "_蓄力中AB")
    this.macroMap["游聚专用ab连发"] := obm(this, "_游聚专用ab连发")
    this.macroMap["旋转摇"] := obm(this, "_旋转摇")
    this.macroMap["ab"] := obm(this, "_ab")
    this.macroMap["计时"] := obm(this, "_计时")
    this.macroArr := this.macroMap.getKeys()
  }

}

/* 制作战斗回路专用UI
*/
class BatcirUI extends CommonUI {

  DEFAULT_HOTKEYS := "~d"
  EXE_ARRAY := [MyEmu.MAME, MyEmu.XZONE]
  HOTKEY_NUM := 8

  /* 构造函数
  */
  __New(macro){
    super.__New("战斗回路", macro)
    this._preset := OrderMap([
    	"蓝人", 
      ["连发A", "下上A连打", "蓄力A", "前A改", 
      "后跳", "后跳", "旋转摇", "ab"],
      
      "猫女",
      ["连发A", "下上A连打", "蓄力A", "前A连打", 
      "下前A", "后跳", "旋转摇", "ab"],
      
      "鸵鸟", 
      ["连发A", "下上A改", "蓄力A", "前A改", 
      "鸵鸟高浮空", "鸵鸟低浮空", "旋转摇", "ab"],
      
      "植物", 
      ["连发A", "下前A", "蓄力A", "前A改", 
      "植物左右连打", "植物吞人", "旋转摇", "ab"],
      
      "冰人", 
      ["连发A", "下上A连打", "蓄力A", "前A改", 
      "下前A", "后跳", "旋转摇", "ab"],
    ])
    this._macro.UI := this ;绑定UI
  }
  
}

/* 运行当前脚本
*/ if (A_ScriptName == "战斗回路.2.ahk") {
  face := FaceDetector() ;方向检测对象
  macro := BatcirMacro(face, ["q","d"]) ;宏对象,有界面时第二个参数可以随便写但长度不要小于2
  ;新建界面并设定
  ui := BatcirUI(macro)
  ui.setup()
  ui._gameKeyEdit.Text := "q,d"
  ui._exeComboBox.Choose(MyEmu.MAME)
  ui._hotkeyEdit.Text := "~s,~a,x,~f,~e,w,~c,space"
  ui._myGui.opt("+AlwaysOnTop")
  ui._myGui.show("x1100 y330")
  ui.readPresetMenu("蓝人")
}